{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1473ca74-d9e9-4379-9a8b-bd6d62d6f5bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a2685edb-d39d-40e4-9f86-ea28bef4f539",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    "api_key = \"sk-cd94b5279fe4438c8b1876ffa4fe7278\"\n",
    "model = \"qwen-vl-plus\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "07f3f904-e726-4a9e-862c-d49457be22a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "mllm = ChatOpenAI(base_url=base_url, api_key=api_key, model=model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f842bd1c-fe44-4e29-b8f9-b50fe2b29c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages=[\n",
    "    {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
    "                {\"type\": \"image_url\",\n",
    "                 \"image_url\": {\"url\": \"https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg\"}},\n",
    "                \n",
    "            {\"type\": \"text\", \"text\": \"\"\"\n",
    "            请使用300字左右的篇幅描述一下这幅图像的内容!\n",
    "            描述内容将会存入RAG知识库，请尽量符合RAG知识库的特点！\n",
    "            \"\"\"}\n",
    "            ]\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e1de366-9f9b-4537-a914-ac66bca03621",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3742303d-f4a2-4b26-94aa-698eddace9a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = mllm.invoke(input=messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ca737b1f-8f35-4300-b65a-21575b59878c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这幅图像展示了一位女性和一只狗在海滩上互动的温馨场景。女性坐在沙滩上，穿着格子衬衫和黑色裤子，面带微笑，显得非常开心和放松。她的右手与狗的前爪轻轻相触，似乎是在进行“握手”的游戏，这种互动显示出她与狗之间深厚的情感联系和默契。狗是一只浅棕色的拉布拉多犬，戴着彩色的项圈，看起来非常温顺和友好。它正专注地看着女性，姿态优雅地抬起前爪，与女性的手相触。\n",
      "\n",
      "背景是广阔的海洋和天空，海浪轻轻拍打着岸边，远处的海水呈现出柔和的蓝色调，天空则被夕阳染成了温暖的金色，营造出一种宁静而美丽的氛围。阳光从画面右侧斜射过来，给整个场景披上了一层金色的光辉，增强了画面的温暖感和幸福感。沙滩上的沙粒细腻柔软，留下了两人和狗的足迹，记录下了这一刻的美好时光。\n",
      "\n",
      "这幅图像不仅捕捉到了人与宠物之间的亲密关系，还展现了自然美景与人类情感的和谐共存，传递出一种轻松愉快的生活态度。整体画面构图简洁而富有情感，光线运用得当，使得每一个细节都显得生动而真实。\n"
     ]
    }
   ],
   "source": [
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2c0b43cb-e55a-486d-a6c1-7daab37ebbb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "93234df9-4679-487c-ba88-34e663e88cbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jpg'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_name = \"大模型连接.jpg\"\n",
    "file_name.split(\".\")[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2ca74ecf-8192-46bd-97a0-8058e0d38d56",
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "import os\n",
    "\n",
    "def image_to_base64(image_path):\n",
    "    \"\"\"\n",
    "    读取图像文件并返回Base64编码字符串\n",
    "    \n",
    "    参数:\n",
    "        image_path (str): 图像文件路径\n",
    "        \n",
    "    返回:\n",
    "        str: 图像的Base64编码字符串\n",
    "    \"\"\"\n",
    "    try:\n",
    "        # 以二进制模式读取图像文件\n",
    "        with open(file=image_path, mode=\"rb\") as image_file:\n",
    "            # 读取图像数据\n",
    "            image_data = image_file.read()\n",
    "            # 转换为Base64编码\n",
    "            base64_str = base64.b64encode(image_data).decode('utf-8')\n",
    "            mime_type = image_path.split(\".\")[-1]\n",
    "            return f\"data:{mime_type};base64,{base64_str}\"\n",
    "    except FileNotFoundError:\n",
    "        print(f\"错误：文件 {image_path} 未找到\")\n",
    "        return None\n",
    "    except Exception as e:\n",
    "        print(f\"发生错误: {str(e)}\")\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e44f9a73-4f2d-4930-a3c8-6d00a72a7460",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages=[\n",
    "    {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
    "            {\"type\": \"image_url\",\n",
    "                 \"image_url\": image_to_base64(image_path=\"dog.jpg\")},\n",
    "             {\"type\": \"image_url\",\n",
    "                 \"image_url\": image_to_base64(image_path=\"cat.jpg\")},\n",
    "                \n",
    "            {\"type\": \"text\", \"text\": \"\"\"\n",
    "            请对比一下这两个图有什么不同\n",
    "            \"\"\"}\n",
    "            ]\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ba6b2a65-5822-466b-aa67-66f96fe6bcc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这两张图片展示了两种不同的宠物，它们在外观、姿态和背景方面有所不同：\n",
      "\n",
      "1. **动物种类**：\n",
      "   - 第一张图是一只柯基犬（Corgi），属于犬科。\n",
      "   - 第二张图是一只长毛猫，属于猫科。\n",
      "\n",
      "2. **外观特征**：\n",
      "   - **柯基犬**：柯基犬有独特的短腿、长身体的体型。它的毛色通常是红白相间的，耳朵竖立，表情活泼可爱。从图片中可以看到它张开嘴巴，露出舌头，显得非常开心。\n",
      "   - **长毛猫**：这只猫有着浓密的长毛，毛色主要是灰色和白色相间。它的耳朵较大且直立，眼睛大而圆，整体看起来比较优雅和安静。\n",
      "\n",
      "3. **姿态**：\n",
      "   - **柯基犬**：柯基犬站立着，面向镜头，显得很友好和活泼。\n",
      "   - **长毛猫**：长毛猫坐着，前爪微微抬起，目光直视镜头，给人一种冷静或好奇的感觉。\n",
      "\n",
      "4. **背景环境**：\n",
      "   - **柯基犬**：背景是户外的砖块地面，旁边有一些绿色的草地。\n",
      "   - **长毛猫**：背景是室内的木质地板，旁边有一部分家具的一部分（如椅子腿）。\n",
      "\n",
      "5. **情绪表现**：\n",
      "   - **柯基犬**：表现出快乐和友好的情绪，似乎在享受与人互动的时刻。\n",
      "   - **长毛猫**：表现出一种平静或略带好奇的情绪，没有明显的兴奋或紧张表现。\n",
      "\n",
      "总的来说，两张图片中的宠物分别代表了不同的性格特点和情感状态，柯基犬更显活泼和亲近人类，而长毛猫则显得更加沉稳和独立。\n"
     ]
    }
   ],
   "source": [
    "response = mllm.invoke(input=messages)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6f23937e-9de3-4361-bf5d-add0776a4a9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "import os\n",
    "\n",
    "def video_to_base64(video_path):\n",
    "    \"\"\"\n",
    "    读取图像文件并返回Base64编码字符串\n",
    "    \n",
    "    参数:\n",
    "        video_path (str): 视频文件路径\n",
    "        \n",
    "    返回:\n",
    "        str: 视频的Base64编码字符串\n",
    "    \"\"\"\n",
    "    try:\n",
    "        # 以二进制模式读取视频文件\n",
    "        with open(file=video_path, mode=\"rb\") as video_file:\n",
    "            # 读取图像数据\n",
    "            video_data = video_file.read()\n",
    "            # 转换为Base64编码\n",
    "            base64_str = base64.b64encode(video_data).decode('utf-8')\n",
    "            mime_type = video_path.split(\".\")[-1]\n",
    "            return f\"data:{mime_type};base64,{base64_str}\"\n",
    "    except FileNotFoundError:\n",
    "        print(f\"错误：文件 {image_path} 未找到\")\n",
    "        return None\n",
    "    except Exception as e:\n",
    "        print(f\"发生错误: {str(e)}\")\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "bffd0600-92d7-4da5-bd8b-982b03c1e293",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages=[\n",
    "    {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
    "            {\"type\": \"video_url\",\n",
    "                 \"video_url\": video_to_base64(video_path=\"1.mp4\")},\n",
    "                \n",
    "            {\"type\": \"text\", \"text\": \"\"\"\n",
    "            简要描述一下这个视频\n",
    "            \"\"\"}\n",
    "            ]\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4f18e75a-0e35-4bff-985d-f70a246d0684",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这个视频展示了一个小孩坐在床上，手里拿着一本打开的书。小孩穿着浅蓝色的无袖上衣和粉色的裤子，戴着一副黑色的眼镜。她正在专注地翻阅书页，看起来非常投入。背景中可以看到一个带有栏杆的床铺，床上铺着花纹毯子，旁边散落着一些衣物和其他物品。房间的光线柔和，营造出一种温馨的家庭氛围。小孩的动作显得很自然，似乎在享受阅读的乐趣。整个视频给人一种宁静、可爱的感觉。\n"
     ]
    }
   ],
   "source": [
    "response = mllm.invoke(input=messages)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1adb78d-d48c-4761-b6f5-21320943b966",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46ebb3de-27c8-4c0c-9e4d-8eaa6d02871c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
